import numpy as np

max_epoch = 100
learning_rate = 1 # 0.001

# training data
# xs = list(range(1, 4))
# ys = xs * 2
# print(xs)
# print(ys)

xs = np.array([1, 2, 3])
ys = xs * 2
print(xs)
print(ys)

class Model():
    def __init__(self, w=0):
        self.w = w

    def update(self, new_w):
        self.w = new_w

    def forward(self, x):
        return self.w * x # predicted y

    def gradient(self, x, y):
        return 2 * x * (self.w * x - y)

    def loss(self, x, y):
        return (self.forward(x) - y) ** 2

model = Model(10)
# train the model
for epoch in range(max_epoch):
    step = 0
    for x, y in zip(xs, ys):
        grad = model.gradient(x, y)
        new_w = model.w - learning_rate * grad
        model.update(new_w)
        loss = model.loss(x, y)
        if step % 10 == 0:
            print("Loss: {:.4f}".format(loss))

        step += 1

# test
y_pred = model.forward(5)
print(y_pred)












